package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.OrderUserAddressAdapter;
import com.beiing.tianshuai.tianshuai.app.AppConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDataBean;
import com.beiing.tianshuai.tianshuai.mine.presenter.OrderAddressPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.OrderAddressViewImpl;
import com.beiing.tianshuai.tianshuai.rx.RxBus;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomEmptyLayout;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;

import java.util.List;

import butterknife.BindView;

public class OrderAddressChooseActivity extends BaseActivity implements OrderAddressViewImpl {

    //加载过渡视图
    @BindView(R.id.cel_empty_view_order_address_choose)
    CustomEmptyLayout mCustomEmptyLayout;
    //Toolbar返回按钮
    @BindView(R.id.toolbar_navigation_back)
    ImageButton toolbarNavigationBack;
    //Toolbar编辑地址
    @BindView(R.id.toolbar_filter_more)
    ImageButton toolbarFilterMore;
    //地址列表RecyclerView
    @BindView(R.id.rlv_address_choose)
    RecyclerView rlvAddressChoose;
    //添加地址布局
    @BindView(R.id.rl_add_address)
    RelativeLayout rlAddAddress;

    private String mUserId;

    private OrderAddressPresenter mPresenter;

    private OrderUserAddressAdapter mAdapter;

    private RxBus rxBus;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_address_choose;
    }

    @Override
    protected void init() {
        mCustomEmptyLayout.setInProgress();
        mUserId = UserInfoBean.getUid(mContext);
        initRecyclerView();
        initPresenter();
        initListener();
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        mAdapter.setOnItemClickListener(new OrderUserAddressAdapter.OnItemClickListener() {
            @Override
            public void onItemSelected(int position, OrderUserAddressDataBean.DataBean.AddressBean bean) {
                List<OrderUserAddressDataBean.DataBean.AddressBean> list = mAdapter.getList();
                // 找到当前默认的地址，设置它的选择状态为false
                for (OrderUserAddressDataBean.DataBean.AddressBean addressBean : list) {
                    if (addressBean.isSelected()) {
                        addressBean.setSelected(false);
                        break;
                    }
                }
                // 设置当前用户选择的item，设置他的选择状态为true
                list.get(position).setSelected(true);
                mAdapter.notifyDataSetChanged();
            }
        });
        //添加地址
        rlAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderAddressEditActivity.start(mContext,Boolean.FALSE);
                //Intent intent = new Intent(mContext, OrderAddressEditActivity.class);
                //intent.putExtra("isEditAddress",false);
                //context.startActivity(intent);
                //startActivityForResult(intent,AppConstant.REQUEST_CODE_EDIT_RECEIVE_ADDRESS);
            }
        });
        toolbarNavigationBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbarFilterMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //showShortText("管理收货地址");
                Intent intent = new Intent(mContext, OrderAddressManageActivity.class);
                //请求码，标记请求
                startActivityForResult(intent,AppConstant.REQUEST_CODE_EDIT_RECEIVE_ADDRESS);
            }
        });

    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        //创建LinearLayoutManager 对象
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        //设置RecyclerView 布局
        rlvAddressChoose.setLayoutManager(mLayoutManager);
        rlvAddressChoose.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, DensityUtils.dp2px(mContext, 0.5f),
                ContextCompat.getColor(mContext, R.color.common_gray_background)));
        //设置Adapter
        mAdapter = new OrderUserAddressAdapter(mContext);
        rlvAddressChoose.setAdapter(mAdapter);
    }

    /**
     * 要请求的用户id
     */
    public void initPresenter() {
        mPresenter = new OrderAddressPresenter(this);
        mPresenter.getUserAddress(UserInfoBean.getUid(mContext));
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    /**
     * 请求数据成功，将数据加载到ricycleview中
     *
     *  @param bean 请求的数据集
     */
    @Override
    public void onRequestSuccess(OrderUserAddressDataBean bean) {
        int code = bean.getCode();
        switch (code) {
            case 200:
                List<OrderUserAddressDataBean.DataBean.AddressBean> addressBeans = bean.getData().getAddress();
                for (OrderUserAddressDataBean.DataBean.AddressBean addressBean : addressBeans) {
                    if (TextUtils.equals(addressBean.getDef(), "1")) {
                        addressBean.setSelected(true);
                        break;
                    }
                }
                mAdapter.updateData(addressBeans);
                break;
            case 401:
                showShortText("参数错误");
                break;
            case 202:
                //mAdapter.notifyDataSetChanged();
                showShortText("没有数据");
                break;
                default:break;
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {

    }

    @Override
    public void getOrderInformationSuccess(OrderUserAddressDataBean bean) {

    }

    @Override
    public void getOrderInformationError(OrderUserAddressDataBean bean) {

    }
    /**
     *     获取回跳方法
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.i("调用了onActivityResult","1234==========================");
        //showShortText("调用了onActivityResult");

        //请求码
        switch (requestCode) {
            case AppConstant.REQUEST_CODE_EDIT_RECEIVE_ADDRESS:
                //结果码
                switch (resultCode) {
                    case AppConstant.RESULT_CODE_EDIT_RECEIVE_ADDRESS:
                        //刷新数据
                        Log.i("获取到了请求码"+AppConstant.RESULT_CODE_EDIT_RECEIVE_ADDRESS,"1234");
                        if (mPresenter == null) {
                            mPresenter = new OrderAddressPresenter(this);
                        }
                        mPresenter.getUserAddress(UserInfoBean.getUid(mContext));
                        break;
                    case 2:
                        //刷新数据，由编辑地址界面返回
                        if (mPresenter == null) {
                            mPresenter = new OrderAddressPresenter(this);
                        }
                        mPresenter.getUserAddress(UserInfoBean.getUid(mContext));
                        break;
                    default:break;

                }
                break;
            default:break;

        }
    }
}